package com.wolves.career.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wolves.career.entity.UserPermissionsAccount;
import com.wolves.career.entity.vo.QueryPermissionsAccount;
import com.wolves.career.mapper.UserPermissionsAccountMapper;
import com.wolves.career.service.UserPermissionsAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-19
 */
@Service
public class UserPermissionsAccountServiceImpl
        extends ServiceImpl<UserPermissionsAccountMapper, UserPermissionsAccount>
        implements UserPermissionsAccountService {
    @Autowired
    private UserPermissionsAccountMapper userPermissionsAccountMapper;

    /**
     * @param queryPermissionsAccount 封装条件查询对象
     * @return 返回条件查询对象wrapper
     */
    @Override
    public QueryWrapper<UserPermissionsAccount>
    getPermissionsAccountQueryWrapper(QueryPermissionsAccount queryPermissionsAccount) {
        //获取所有条件查询对象
        String username = queryPermissionsAccount.getUsername();
        String account = queryPermissionsAccount.getAccount();
        Integer telephone = queryPermissionsAccount.getTelephone();
        Integer type = queryPermissionsAccount.getType();
        Date updateTime = queryPermissionsAccount.getUpdateTime();
        Date createTime = queryPermissionsAccount.getCreateTime();
        QueryWrapper<UserPermissionsAccount> queryWrapper = new QueryWrapper<>();
        //如果参数不为空就将参数加入
        if (!StringUtils.isEmpty(username)) {
            //模糊查询姓名 like
            queryWrapper.like("username", username);
        }
        if (!StringUtils.isEmpty(account)) {
            queryWrapper.eq("account", account);
        }
        if (!StringUtils.isEmpty(telephone)) {
            queryWrapper.eq("telephone", telephone);
        }
        if (!StringUtils.isEmpty(updateTime)) {
            queryWrapper.lt("update_time", updateTime);
        }
        if (!StringUtils.isEmpty(createTime)) {
            queryWrapper.gt("create_time", createTime);
        }
        if (!StringUtils.isEmpty(type)) {
            queryWrapper.eq("type", type);
        }
        return queryWrapper;
    }

    @Override
    public Boolean isMaintenancePersonnel(String accountId) {
        UserPermissionsAccount user = userPermissionsAccountMapper.selectById(accountId);
        if ((user.getType() == 1)) {
            //type不是1，不是维护人员
            return true;
        }
        return false;
    }

    @Override
    public boolean updatePassword(String accountId, String oldPassword, String newPassword) {
        if (newPassword.equals(oldPassword)) {
            //如果新旧密码重复就返回false
            return false;
        }
        //先查找该用户
        UserPermissionsAccount user = userPermissionsAccountMapper.selectById(accountId);
        user.setPassword(newPassword);
        //更新用户密码
        int count = userPermissionsAccountMapper.updateById(user);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean exist(String accountId, String password) {
        QueryWrapper<UserPermissionsAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", accountId);
        queryWrapper.eq("password", password);
        UserPermissionsAccount user = userPermissionsAccountMapper.selectOne(queryWrapper);
        if (user != null) {
            return true;
        }
        return false;
    }

    @Override
    public boolean updateUsername(String accountId, String newUsername) {
        UserPermissionsAccount user = userPermissionsAccountMapper.selectById(accountId);
        user.setUsername(newUsername);
        int count = userPermissionsAccountMapper.updateById(user);
        if (count > 0) {
            return true;
        } return false;
    }

    @Override
    public boolean updateTelephone(String accountId, Integer newTelephone) {
        UserPermissionsAccount user = userPermissionsAccountMapper.selectById(accountId);
        user.setTelephone(newTelephone);
        int count = userPermissionsAccountMapper.updateById(user);
        if (count>0){
            return true;
        }return false;
    }

}
